/**
 * 
 */
package de.arnoldmedia.mybookshelf.resources;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.MessageFormat;
import java.util.Properties;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.skype.SkypeException;

import de.arnoldmedia.mybookshelf.IMyBookshelf;
import de.arnoldmedia.mybookshelf.resources.model.Bookshelf;

/**
 * @author aarnold
 * 
 */
public class ResourceHandler {

	/**
	 * Listener Instance which listen for incoming connections
	 * 
	 * @category attribute
	 */
	private Properties properties = null;

	/**
	 * 
	 */
	public Bookshelf bookshelf = null;

	/**
	 * @throws IOException
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @throws FileNotFoundException
	 * @throws SkypeException
	 * @throws JAXBException
	 * 
	 */
	public ResourceHandler(IMyBookshelf parent) throws FileNotFoundException,
			IOException {

		// load default properties from file
		this.properties = new Properties();
		this.properties.load(new FileInputStream(Constants.FILENAME_PROPERTY));

		// check if assertions are required by property flag
		if (!this.checkAssertionsEnabled())
			throw new RuntimeException(
					"Asserts must be enabled!!! (-enableassertions)");

		// load language depended properties
		this.properties.load(new FileInputStream(MessageFormat.format(
				Constants.FILENAME_I18N_PROPERTY, this
						.getPropertyString(Constants.PROPERTY_LANG))));

		// TODO println debug only
		System.out.println("Application language set to "
				+ this.getPropertyString(Constants.I18N_LANGUAGE));

		this.bookshelf = new Bookshelf();

	}// ResourceHandler

	// ############## business methods ##################

	public void loadBookshelf() throws JAXBException, FileNotFoundException {

		// create JAXB context and instantiate marshaller
		Unmarshaller unmarshaller = JAXBContext.newInstance(
				"de.arnoldmedia.mybookshelf.resources.model")
				.createUnmarshaller();

		// read content from xml file
		this.bookshelf = (Bookshelf) unmarshaller.unmarshal(new FileReader(
				Constants.FILENAME_JAXB_XML));

	}// loadBookshelf

	public void saveBookshelf() throws JAXBException, IOException {

		// create JAXB context and instantiate marshaller
		Marshaller marshaller = JAXBContext.newInstance(
				"de.arnoldmedia.mybookshelf.resources.model")
				.createMarshaller();

		// write content to xml file
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		marshaller.marshal(this.bookshelf, System.out);

		Writer w = null;
		try {
			w = new FileWriter(Constants.FILENAME_JAXB_XML);
			marshaller.marshal(this.bookshelf, w);
		} finally {
			w.close();
		}

	}// storeBookshelf

	public String getPropertyString(String key) {
		return this.properties.getProperty(key);
	}// getProperty

	public boolean getPropertyBoolean(String key) {

		// read string from property file
		String property_str = this.properties.getProperty(key);

		// value must be either true or false
		assert (property_str.contentEquals("true") || property_str
				.contentEquals("false"));

		// convert string to real boolean
		Boolean property_bool = Boolean.parseBoolean(property_str);

		// return boolean
		return property_bool;
	}// getProperty

	/**
	 * ensure that assertions are not disabled
	 * http://java.sun.com/j2se/1.5.0/docs/guide/language/assert.html
	 * 
	 * @return
	 */
	private boolean checkAssertionsEnabled() {

		boolean assertsEnabled = false;
		if (this.getPropertyBoolean(Constants.PROPERTY_ASSERT)) {

			// check if JavaVM Assertions are enabled
			assert assertsEnabled = true; // Intentional side effect!!!

		} else {

			// assertion check disabled by property
			assertsEnabled = true;

		}// if
		return assertsEnabled;

	}// checkAssertionsEnabled

}// class
